<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				text="登 录"
				:rotate="isRotate" 
				@click.native="getLogin()"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	var loginResult;
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				username:'',
				password:'',
				loginState:false
			};
		},
		components:{
			wInput,
			wButton,
		},

		onLoad:function(options){


		},
		methods: {
			getLogin(){
				uni.showLoading({
					title:'正在登录'
				})
				
				uni.request({
					url: this.createApiUrl('user/login'),
					method: 'POST',
					header: {
					        'content-type': 'application/x-www-form-urlencoded'  
					},
					data: {
						username:this.phoneData,
						password:this.passData
					},
					success: res => {
						console.log('login->res: ');
						
						uni.hideLoading();
						
						res = res.data;
						uni.setStorageSync('userinfo',res.data);

						console.log(res);
						if(res.code == 200){
							uni.showToast({
								title:'登录成功'
							})
							uni.setStorageSync('SUID', res.data.id + '');
							uni.setStorageSync('SRAND', Math.random(10) + '');
							uni.setStorageSync('SNAME', res.data.username + '');
							uni.setStorageSync('SFACE', res.data.nickname + '');
							console.log('nickname:' + res.data.nickname);
						}else{
							uni.showToast({
								title:'登录失败'
							})
						}

						setTimeout(function(){
							uni.switchTab({
								url:'/pages/tabbar/tabbar-1/tabbar-1'
							})
						}, 1000);
					},
					fail: () => {},
					complete: () => {}
				});
			},
		    
			
			
			getLoginState(){
				return this.loginState;
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
